#{extends 'main.html' /} #{set title:'Start Action Plan' /}
<head>

<script type="text/javascript">
    
    function startActionPlans(){
    
        var name = document.getElementById("name").value;
        var desc = document.getElementById("desc").value;
        var startDay = document.getElementById("startDay").value;
        var startMonth = document.getElementById("startMonth").value;
        var startYear = document.getElementById("startYear").value;
        var endDay = document.getElementById("endDay").value;
        var endMonth = document.getElementById("endMonth").value;
        var endYear = document.getElementById("endYear").value;
        var idea = document.getElementById("ideaId").value;
        var organizer = document.getElementById("organizerId").value;
        var n = true;
        var description = true;
        var sdate = true;
        var edate = true;
        
        if (name.length == 0) {
			document.getElementById("nameError").innerHTML = "You must enter a name";
            name = false;
        }
        if (startDay.length == 0 || startMonth.length == 0 || startYear.length == 0) {
 
	document.getElementById("startDateError").innerHTML = "You must enter a start date";
           
        } else {
		
		 if ((parseInt(startDay) == NaN) || (parseInt(startMonth) == NaN) || (parseInt(startYear) == NaN)) {
				document.getElementById("startDateError").innerHTML = "You must enter a valid start date";
                sdate = false;
            }
            else {
                if (!correctDate(startMonth, startDay, startYear)) {
                    document.getElementById("startDateError").innerHTML = "You must enter a valid start date";					
                    sdate = false;
                }
            }		
		}
		
        if (endDay.length == 0 || endMonth.length == 0 || endYear.length == 0) {
			document.getElementById("endDateError").innerHTML = "You must enter an end date";
            
        } else {
			if ((parseInt(endDay) == NaN) || (parseInt(endMonth) == NaN) || (parseInt(endYear) == NaN)) {
                document.getElementById("endDateError").innerHTML = "You must enter a valid end date";	
                edate = false;
            }
            else {
                if (!correctDate(endMonth, endDay, endYear)) {
                    document.getElementById("endDateError").innerHTML = "You must enter a valid end date";	
                    edate = false;
                }
            }
		}
		
		
		
		
        if (desc.length == 0) {
			document.getElementById("desError").innerHTML = "You must enter a description";	
            description = false;
        }
        
        if (n && description && sdate && edate) {
        
            $.getJSON('@{ActionPlanController.StartActionPlanM()}', {
                'name': name,
                'ideaId': idea,
                'description': desc,
                'startDay': startDay,
                'startMonth': startMonth,
                'startYear': startYear,
                'endDay': endDay,
                'endMonth': endMonth,
                'endYear': endYear,
                'organizerId': organizer
            }, function(data){
				
				if(data.res=="Action plan started"){
					
					$('#one').jGrowl(data.res)
					
				} else {
					
				document.getElementById("error").innerHTML = data.res;
				
				}
            });
        }
    }
    
    function correctDate(month, day, year){
    
        if (month == 1 || month == 3 || month == 5 || month == 7 ||
        month == 8 ||
        month == 10 ||
        month == 12) {
            if ((day > 0) && (day < 32)) {
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if (month == 2) {
                if (day > 0 && day < 30) {
                    return true;
                }
                else {
                    return false;
                }
            }
            else {
                if (month == 4 || month == 6 || month == 9 || month == 11) {
                    if (day > 0 && day < 31) {
                        return true;
                    }
                    else {
                        return false;
                    }
                }
            }
        }
    }
</script>
</head>
<body>
    <style>
        .inputHighlighted {
            color: #ff0000;
        }
    </style>
    <div id="content">
        <h2>Start action plan</h2>
        <form name="inputs" action="">
            <fieldset>
                <legend>
                </legend>
                <p>
                    <label for="Name">
                        Name: 
                    </label>
                    <input name="name" id="name" value="" type="text" />
                    <spanF class="inputHighlighted">
                    *</span>
                    <span class="inputHighlighted" id="nameError"></span>
                </p>
                <p>
                    <label for="startDate">
                        Start date: 
                    </label>
                    <INPUT TYPE="text" id="startDay" VALUE="" SIZE=2> / <INPUT TYPE="text" id="startMonth" VALUE="" SIZE=2> / <INPUT TYPE="text" id="startYear" VALUE="" SIZE=4>(mm/dd/yyyy) 				
               		<spanF class="inputHighlighted">
                    *</span>
					<span class="inputHighlighted" id="startDateError"></span>
			    </p>
                <p>
                    <label for="endDate">
                        End date: 
                    </label>
                    <INPUT TYPE="text" id="endDay" VALUE="" SIZE=2> / <INPUT TYPE="text" id="endMonth" VALUE="" SIZE=2> / <INPUT TYPE="text" id="endYear" VALUE="" SIZE=4>(mm/dd/yyyy) 				
                	<spanF class="inputHighlighted">
                    *</span>
					<span class="inputHighlighted" id="endDateError"></span>
				</p>
                <p>
                    <label for="b">
                        Description: 
                    </label>
                    <input name="desc" id="desc" value="" type="text" />
                    <spanF class="inputHighlighted">
                    *</span>
                    <span class="inputHighlighted" id="desError"></span>
                </p>
                <p>
                    <span class="inputHighlighted" id="error"></span>
                </p>
                <p>
                    <p>
                        <input name="startButton" style="margin-left: 150px;" class="formbutton" value="Start" type="button" onclick="javascript:startActionPlans()"/>
                    </p>
                </p>
            </fieldset>
        </form><input id="ideaId" type="hidden" value="${ideaId}" /><input id="organizerId" type="hidden" value="${organizerId}" />
    </div>
</body>